package com.qjz.web.servlet;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class FileDownloadExcelServlet extends HttpServlet {

	private static final long serialVersionUID = 1194256751045811176L;

	private static final Log LOGGER = LogFactory
			.getLog(FileDownloadExcelServlet.class);

	private static final String CHARCHTER_ENCODING = "UTF-8";

	public void init() throws ServletException {
		super.init();
	}

	/**
	 * 
	 * <code>GET</code> 和 <code>POST</code>的共同上传方法
	 * 
	 * @param request
	 *            servlet request
	 * 
	 * @param response
	 *            servlet response
	 * @throws ServletException
	 *             Servlet异常
	 * @throws IOException
	 *             IO异常
	 */
	public void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		LOGGER.info("==============ExcelServlet 启动==============");
		response.setCharacterEncoding(CHARCHTER_ENCODING);

		String filePath = request.getParameter("filePath");
		String fileName = new String(request.getParameter("fileName").getBytes(
				"iso-8859-1"), CHARCHTER_ENCODING);

		if (null == filePath || "".equals(filePath)) {
			return;
		}

		String webRoot = getServletContext().getRealPath("/");
		//System.getProperty("file.separator");
		
		/*if (!webRoot.endsWith("//")) {
			webRoot += "//";
		}*/
		
		FileInputStream fis = null;
		OutputStream toClient = null;

		try {
			File inputFile = new File(webRoot + "excel" + System.getProperty("file.separator") + fileName);
			System.out.println(webRoot + "excel" + System.getProperty("file.separator") + fileName);
			fis = new FileInputStream(inputFile);
			// fis = FileOperate.getFileInputStraem(filePath + "\\" + fileName);
			response.reset();
			response.addHeader("Content-Disposition", "attachment;fileName="
					+ new String(fileName.getBytes(), "iso-8859-1"));
			response.addHeader("Content-Length", "" + inputFile.length());
			response.setContentType("application/octet-stream");

			toClient = new BufferedOutputStream(response.getOutputStream());

			byte[] buff = new byte[1024];
			while (fis.read(buff) != -1) {
				toClient.write(buff);
			}
			toClient.flush();

		} catch (FileNotFoundException e) {
			LOGGER.error("文件未找到！");
			// out.write("文件未找到, 请联系管理员！")
			// System.out.println((e.getMessage()));
		} finally {
			if (null != toClient) {
				toClient.close();
			}
			if (null != fis) {
				fis.close();
			}
			// out.flush();
			LOGGER.info("==============ExcelServlet 结束==============");
		}
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response)

	throws ServletException, IOException {

		processRequest(request, response);

	}

	/** */
	/**
	 * 
	 * Handles the HTTP <code>POST</code> method.
	 * 
	 * @param request
	 *            servlet request
	 * 
	 * @param response
	 *            servlet response
	 */

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response)

	throws ServletException, IOException {

		processRequest(request, response);

	}

	/**
	 * Returns a short description of the servlet.
	 * 
	 * @return String
	 */
	public String getServletInfo() {

		return "Short description";

	}
}
